import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:studymanage/core/global.dart';
import 'package:studymanage/course/course_intro.dart';
import 'package:studymanage/util/request.dart';
class MyCourse extends StatefulWidget {
  MyCourse({Key key, this.title}) : super(key: key);

  final String title;

  @override
  State<StatefulWidget> createState() {
    return MyCourseState();
  }
}

class MyCourseState extends State<MyCourse> {
  ScrollController _scrollController = ScrollController();
  List _studentCourseList = [];
  int _currentType = 1;
  bool isLoading = false;
  int _page=1;
  @override
  void initState() {
    _getData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          elevation: 0,
          backgroundColor: Colors.white,
          title: Text("我的课程",style: TextStyle(color: Color.fromRGBO(51, 51, 51, 1)),),
          leading: FlatButton(
            child: Icon(
              Icons.arrow_back_ios,
              color: Colors.grey,
            ),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
        ),
        body: Column(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(top: 10),
              decoration: BoxDecoration(
                color: Colors.white,
                border: Border(
                  bottom: BorderSide(color: Colors.grey,width: 0.1),

                )
              ),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceAround,
                children: <Widget>[
                  GestureDetector(
                    onTap: (){
                      setState(() {
                        _currentType =1;
                        _studentCourseList = [];
                        _page = 1;
                        isLoading =false;
                      });
                      _getData();
                    },
                    child: Column(
                      children: <Widget>[
                        Container(
                          padding: EdgeInsets.all(20),
                          child: Text("全部",style: TextStyle(fontSize: 14,color: _currentType == 1 ? Color(0xffDDB078) : Color(0xff333333)),)
                        ),
                        Container(
                          width: 30,
                          decoration: BoxDecoration(
                            border: _currentType == 1 ?  Border(
                              bottom: BorderSide(
                                color: Color(0xffDDB078),
                                width: ScreenUtil().setHeight(1),
                              ),
                            ):null
                          ),
                        ),
                      ],
                    ),
                  ),
                  GestureDetector(
                    onTap: (){
                      setState(() {
                        _currentType =2;
                        _studentCourseList = [];
                        _page = 1;
                        isLoading =false;
                      });
                      _getData();
                    },
                    child: Column(
                      children: <Widget>[
                        Container(
                            padding: EdgeInsets.all(20),
                            child: Text("学习中",style: TextStyle(fontSize: 14,color: _currentType == 2 ? Color(0xffDDB078) : Color(0xff333333)))
                        ),
                        Container(
                          width: 30,
                          decoration: BoxDecoration(
                              border: _currentType == 2 ?  Border(
                                bottom: BorderSide(
                                  color: Color(0xffDDB078),
                                  width: ScreenUtil().setHeight(1),
                                ),
                              ):null
                          ),
                        ),
                      ],
                    ),
                  ),
                  GestureDetector(
                    onTap: (){
                      setState(() {
                        _currentType =3;
                        _studentCourseList = [];
                        _page = 1;
                        isLoading =false;
                      });
                      _getData();
                    },
                    child: Column(
                      children: <Widget>[
                        Container(
                            padding: EdgeInsets.all(20),
                            child: Text("已完成",style: TextStyle(fontSize: 14,color: _currentType == 3 ? Color(0xffDDB078) : Color(0xff333333)))
                        ),
                        Container(
                          width: 30,
                          decoration: BoxDecoration(
                              border: _currentType == 3 ?  Border(
                                bottom: BorderSide(
                                  color: Color(0xffDDB078),
                                  width: ScreenUtil().setHeight(1),
                                ),
                              ):null
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),
            Expanded(
              child:  SingleChildScrollView(
                child: Container(
                  padding: EdgeInsets.only(top: 10),
                  decoration: BoxDecoration(
                    color: Colors.white
                  ),
                  child: ListView.builder(
                    shrinkWrap: true,
                    physics: NeverScrollableScrollPhysics(),
                    itemCount: _studentCourseList.length,
                    itemBuilder: (context, index) {
                      return Column(
                        children: <Widget>[
                          GestureDetector(
                            child: Container(
                              width: MediaQuery.of(context).size.width,
                              height: ScreenUtil().setWidth(83.5),
                              padding: EdgeInsets.only(
                              ),
                              margin: EdgeInsets.only(
                                left: ScreenUtil().setWidth(13),
                                right: ScreenUtil().setWidth(13),
                              ),
                              decoration: BoxDecoration(),
                              child: Row(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: <Widget>[
                                  Container(
                                    width: ScreenUtil().setWidth(83),
                                    height: ScreenUtil().setWidth(83),
                                    decoration: BoxDecoration(
                                      image: DecorationImage(
                                        image: NetworkImage(_studentCourseList[index]['insetCoverUrl'],),
                                        fit: BoxFit.fill,
                                      ),
                                      borderRadius: BorderRadius.all(Radius.circular(ScreenUtil().setHeight(4))),
                                    ),
                                  ),
                                  Container(
                                    width: MediaQuery.of(context).size.width - ScreenUtil().setWidth(125),
                                    height: ScreenUtil().setHeight(83),
                                    margin: EdgeInsets.only(
                                      left: ScreenUtil().setWidth(14.5),
                                    ),
                                    child: Column(
                                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: <Widget>[
                                        Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: <Widget>[
                                            Text(_studentCourseList[index]['courseName'],
                                              maxLines: 1,
                                              overflow: TextOverflow.ellipsis,
                                              style: TextStyle(
                                                fontFamily: 'PingFangSC-Medium',
                                                fontSize: ScreenUtil().setSp(14),
                                                color: Color.fromRGBO(51, 51, 51, 1),
                                                fontWeight: FontWeight.w600,
                                                height: 1,
                                              ),
                                            ),
                                            Container(
                                              margin: EdgeInsets.only(
                                                top: ScreenUtil().setHeight(5),
                                              ),
                                              child: Text(
                                                _studentCourseList[index]['courseIntro'],
                                                maxLines: 2,
                                                style: TextStyle(
                                                  fontFamily: 'PingFangSC-Regular',
                                                  fontSize: ScreenUtil().setSp(11),
                                                  fontWeight: FontWeight.w400,
                                                  color: Color.fromRGBO(133, 133, 133, 1),
                                                  height: 1.2,
                                                ),
                                                textAlign: TextAlign.justify,
                                                overflow: TextOverflow.ellipsis,
                                              ),
                                            ),
                                          ],
                                        ),
                                        Container(
                                          margin: EdgeInsets.only(
                                            bottom: ScreenUtil().setHeight(0),
                                          ),
                                          child: Row(
                                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                            crossAxisAlignment: CrossAxisAlignment.center,
                                            children: <Widget>[
                                              Row(
                                                crossAxisAlignment: CrossAxisAlignment.center,
                                                children: <Widget>[
                                                  Icon(Icons.access_time,
                                                    color: Color.fromRGBO(173, 173, 173, 1),
                                                    size: ScreenUtil().setSp(14),
                                                  ),
                                                  Container(
                                                    margin: EdgeInsets.only(
                                                      left: ScreenUtil().setWidth(5),
                                                    ),
                                                    child: Text(_studentCourseList[index]['studyState'] != 1 ? _studentCourseList[index]['courseProgress'] : '未学习',
                                                      style: TextStyle(
                                                        fontFamily: 'PingFangSC-Regular',
                                                        fontSize: ScreenUtil().setSp(11),
                                                        fontWeight: FontWeight.w600,
                                                        color: Color.fromRGBO(51, 51, 51, 1),
                                                        height: 1,
                                                      ),
                                                    ),
                                                  ),
                                                ],
                                              ),
                                            ],
                                          ),
                                        )
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                            ),
                            onTap: () {
                              Request().post<Map<String,dynamic>>("/multiapi/c188v_course",data: {"id":_studentCourseList[index]['id']}).then((response) {
                                Navigator.push(context, MaterialPageRoute(builder: (context) => CourseIntro(response.data,_studentCourseList[index]['id'],_studentCourseList[index]['currentCourseSectionId'],()=>_refresh())));
                              });
                            },
                          ),
                          Divider(height: 40,)
                        ],
                      );
                    },
                  ),
                ),
              ),
            )
          ],
        )
    );
  }

  @override
  void dispose() {
    super.dispose();
  }

  _getData(){
    if(isLoading){
      return;
    }
    setState(() {
      isLoading = true;
    });
    Request().post<Map>("/multiapi/c224p_course",
        data: {
          "pageNum": _page,
          "pageSize": 10,
          "queryType": _currentType-1
        }).then((response){
          setState(() {
            if (!response.data["isLastPage"]) {
              isLoading = false;
              _page += 1;
            }
          });
          setState(() {
              _studentCourseList.addAll(response.data['list']) ;
          });
    });

  }

  _refresh(){
    setState(() {
      isLoading = false;
      _page = 1;
      _currentType =0;
      _studentCourseList=[];
    });
    _getData();
  }

}
